<template>

  <el-dialog width="30%" :title="formData.id?'编辑权限':'新增权限'" :visible="showDialog" @close="closeFn">
    <el-form ref="form" label-width="120px" :rules="rules" :model="formData">
      <el-form-item label="权限名称" prop="name">
        <el-input v-model="formData.name" />
      </el-form-item>
      <el-form-item label="权限标识" prop="code">
        <el-input v-model="formData.code" />
      </el-form-item>

      <el-form-item label="权限描述" prop="description">
        <el-input v-model="formData.description" />
      </el-form-item>
      <!--
            switch组件中，默认v-model绑定的值是一个布尔值
            我们evVisible的值却是 "0"和"1"，那么我们需要把v-model绑定值改为
            字符串0和1，如何修改呢？
            答：可以通过以下两个属性进行修改
            active-value：设置开启状态的值
            inactive-value：设置关闭状态的值
         -->
      <el-form-item :label="value?'开启':'关闭'">
        <el-switch v-model="formData.enVisible" inactive-value="0" active-value="1" />
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-row type="flex" justify="center">
        <el-button type="primary" @click="comfirmFn">确定</el-button>
        <el-button @click="closeFn">取消</el-button>
      </el-row>
    </template></el-dialog>
</template>

<script>
import { addPermissions, getPermissionlDetail, updatePermission } from '@/api/permission'

export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false }
  },
  data() {
    return {
      value: true,
      showDepts: false,
      formData: {
        name: '',
        code: '',
        description: '',
        enVisible: '1',
        pid: '',
        type: 1
      },
      rules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入手机号码', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入工号', trigger: 'blur' }
        ]
      }
    }
  },
  async created() {

  },
  methods: {
    async getPermissionlDetail(id) {
      const loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      this.formData = await getPermissionlDetail(id)
      loading.close()
    },
    async comfirmFn() {
      await this.$refs.form.validate()
      if (this.formData.id) {
        await updatePermission(this.formData)
      } else {
        await addPermissions(this.formData)
      }
      this.closeFn()
      this.$emit('updateEvent')
    },
    closeFn() {
      this.$emit('update:showDialog', false)
      this.formData = {
        nname: '',
        code: '',
        description: '',
        enVisible: '1',
        pid: '',
        type: 1
      }
      this.$refs.form.resetFields()
    }
  }
}
</script>

<style lang="scss">
.el-loading-mask.is-fullscreen{
  z-index: 9999999 !important;
}
</style>
